﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ServerPlatform.Web;
using MapEntities;
using ServerPlatform.Exceptions;

namespace WebApp.Controls
{
    public partial class ItemBindingRuleEditor : BaseDataEditor<ItemBindingRule>
    {
        protected override void OnInit(EventArgs e)
        {
            ctlBaseBindingRuleEditor.CreateInstance += new BaseDataEditor<BaseBindingRule>.OnCreateInstance(ctlBaseBindingRuleEditor_CreateInstance);
            base.OnInit(e);
        }

        private ItemBindingRule layerItemRule = null;
        public ItemBindingRule LayerItemRule { get { return layerItemRule; } set { layerItemRule = value; ctlBaseBindingRuleEditor.ItemRule = value; } }

        BaseBindingRule ctlBaseBindingRuleEditor_CreateInstance()
        {
            return this.CreateDefaultInstance();
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public override void Edit(ItemBindingRule item)
        {

            ctlBaseBindingRuleEditor.Edit(item);
            base.Edit(item);
        }

        protected override void ShowEntity(ItemBindingRule entity)
        {
            ctlValueField.Text = entity.ValueField;
        }

        public override ItemBindingRule GetEntity()
        {
            return (ItemBindingRule)ctlBaseBindingRuleEditor.GetEntity();
        }

        public override ItemBindingRule EndEdit()
        {
            ItemBindingRule result = (ItemBindingRule)ctlBaseBindingRuleEditor.EndEdit();
            if (!string.IsNullOrEmpty(ctlValueField.Text.Trim()))
            {
                result.ValueField = ctlValueField.Text;
            }
            else throw new BusinessException("Value field cannot be empty");
            return result;
        }
    }
}